package com.chinasofti.dao;

import com.chinasofti.util.ConnectionFactory;
import com.chinasofti.vo.Employee;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDao {
    private Connection conn;

    public void insert(Employee employee){
        conn = ConnectionFactory.getConnection();
        String sql = "insert into employee " +
                "(employeename,username,password,phone,email,departmentid,status,role)" +
                "values(?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,employee.getEmployeename());
            pst.setString(2,employee.getUsername());
            pst.setString(3, employee.getPassword());
            pst.setString(4,employee.getPhone());
            pst.setString(5,employee.getEmail());
            pst.setInt(6,employee.getDepartmentid());
            pst.setString(7,"0");//注册需要审核
            pst.setString(8,"2");//默认为普通员工
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
    }



    //通过用户名和密码查询用户信息，把查询到的属性封装成Employee返回，没找到返回null
    // 方法名selectBynamePwd
    public Employee selectBynamePwd(String username,String pwd){
        Employee employee = null;
        conn = ConnectionFactory.getConnection();
        PreparedStatement  pst = null;
        String sql = "select * from employee where username=? and password=?";
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,username);
            pst.setString(2,pwd);
            rs = pst.executeQuery();
            if(rs.next()){
                employee = new Employee();
                employee.setEmployeeid(rs.getInt("employeeid"));
                employee.setEmployeename(rs.getString("employeename"));
                employee.setUsername(rs.getString("username"));
                employee.setPhone(rs.getString("phone"));
                employee.setEmail(rs.getString("email"));
                employee.setStatus(rs.getString("status"));
                employee.setDepartmentid(rs.getInt("departmentid"));
                employee.setPassword(rs.getString("password"));
                employee.setRole(rs.getString("role"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
        return employee;
    }

    //通过查询用户
    public Employee selectByUsername(String username){
        Employee employee = null;
        conn = ConnectionFactory.getConnection();
        PreparedStatement  pst = null;
        String sql = "select * from employee where username=?";
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,username);
            rs = pst.executeQuery();
            if(rs.next()){
                employee = new Employee();
                employee.setEmployeeid(rs.getInt("employeeid"));
                employee.setEmployeename(rs.getString("employeename"));
                employee.setUsername(rs.getString("username"));
                employee.setPhone(rs.getString("phone"));
                employee.setEmail(rs.getString("email"));
                employee.setStatus(rs.getString("status"));
                employee.setDepartmentid(rs.getInt("departmentid"));
                employee.setPassword(rs.getString("password"));
                employee.setRole(rs.getString("role"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
        return employee;

    }


    public List<Employee> selectAllEmpyoee() {
        List<Employee> list = new ArrayList<>();
        Employee employee = null;
        conn = ConnectionFactory.getConnection();
        PreparedStatement  pst = null;
        String sql = "select * from employee where role='2' and  status='0'";
        ResultSet rs = null;
        try {
            pst = conn.prepareStatement(sql);

            rs = pst.executeQuery();
            while (rs.next()){
                employee = new Employee();
                employee.setEmployeeid(rs.getInt("employeeid"));
                employee.setEmployeename(rs.getString("employeename"));
                employee.setUsername(rs.getString("username"));
                employee.setPhone(rs.getString("phone"));
                employee.setEmail(rs.getString("email"));
                employee.setStatus(rs.getString("status"));
                employee.setDepartmentid(rs.getInt("departmentid"));
                employee.setPassword(rs.getString("password"));
                employee.setRole(rs.getString("role"));
                list.add(employee);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }
        return list;
    }

    public void updateStatus(int employeeid, String status) {
        conn = ConnectionFactory.getConnection();
        PreparedStatement  pst = null;
        String sql = "update employee set status=? where employeeid=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,status);
            pst.setInt(2,employeeid);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection();
        }

    }
}